#ifndef DATACONVERTER_HPP
#define DATACONVERTER_HPP
#include "dataconverter.h"
template <typename T> T DataConverter::pop(const bool &reverse) {
  return pop<T>(sizeof(T), reverse);
}
template <typename T>
T DataConverter::pop(const int &num, const bool &reverse) {
  assert(frame_buf.size() >= num);
  T ret;
  char *p = reinterpret_cast<char *>(&ret);
  std::memset(p, 0, sizeof(T));
  for (int iN = 0; iN < num; ++iN) {
    int i;
    if (reverse)
      i = num - iN - 1;
    else
      i = iN;
    if (i < 0 || i >= sizeof(T))
      continue;
    switch (type) {
    case BigEndian:
      p[sizeof(T) - iN - 1] = frame_buf[i];
      break;
    case LittleEndian:
      p[iN] = frame_buf[i];
      break;
    }
  }
  frame_buf.remove(0, num);
  if (frame_buf.isEmpty())
    isUpdate = false;
  return ret;
}

template <typename T> void DataConverter::push(const T &val) {
  push(val, sizeof(T));
}
template <typename T> void DataConverter::push(const T &val, const int &num) {
  assert(isWriting);
  const char *p = reinterpret_cast<const char *>(&val);
  QByteArray ary(num, 0);
  for (int iN = 0; iN < num; ++iN) {
    char res = 0;
    if (iN >= 0 && iN < sizeof(T)) {
      res = p[iN];
      switch (type) {
      case LittleEndian:
        res = p[iN];
        break;
      case BigEndian:
        res = p[sizeof(T) - iN - 1];
        break;
      }
    }
    ary[iN] = res;
  }
  frame_buf.append(ary);
}
#endif
